package com.fr.chicken.pay.com.shb.pay.main;

import com.fr.chicken.pay.com.shb.pay.util.HttpUtils;
import com.fr.chicken.pay.com.shb.pay.util.RSAUtils;
import net.sf.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

/***
 * 查询接口
 */
public class QueryOrder {
    /**
     * 查询订单
     * <p>
     * 请求数据 {"businessContext":{"shbOrderNumber":"API01180115145659000910000028"},"businessHead":{"charset":"00","version":"V1.0.0","method":"payment","merchantNumber":"API0100002802000031","requestTime":"20180115175941","signType":"","sign":""}}
     * 响应数据 {"businessContext":{"currenciesType":"CNY","defrayalType":"ALI_NATIVE","merchantOrderNumber":"dsorderid2018011508","orderStatus":"WAIT","shbOrderNumber":"API01180115145659000910000028","tradeAmount":"100","tradeFee":"0"},"businessHead":{"charset":"UTF-8","merchantNumber":"API0100002802000031","method":"payment","responseTime":"20180115175250","signType":"RSA","version":"V1.0.0","sign":"eaok5oLTExW+5gCnUYEXt2MHd/l9crGXWzfE2SYWR5kBeBRMm/t2kxRj9XM5hRtQ++pRetKJelgbt/wO9YFCJoOVqBiXVnU2jjXkhSZ3IWNqp0ILjYKjl4yo4Pj27H/uNG4IuhirFHHRdjcO2WKQv5NsGWT7Pv6uJxK8OVHWqhs="}}
     *
     * @throws Exception
     */
    public void queryOrder() throws Exception {
        JSONObject businessHead = buildBusinessHead();
        JSONObject businessContext = buildBusinessContextFormQuery();
        System.out.println("{\"businessContext\":" + businessContext + ",\"businessHead\":" + businessHead + "}");
        // 加密请求数据
        String reqEncrypt = RSAUtils.verifyAndEncryptionToString(businessContext, businessHead, PayConfig.MCH_PRIVATE_KEY, PayConfig.SHB_PUBLIC_KEY);
        JSONObject context = new JSONObject();
        context.put("context", reqEncrypt);
        System.out.println(context.toString());
        String respEncrypt = HttpUtils.httpPost(PayConfig.HOST_URL + PayConfig.QUERY_ORDER_URL, context.toString());
        System.out.println(respEncrypt);

        JSONObject respJson = JSONObject.fromObject(respEncrypt);
        if (respJson.get("success").toString().equals("false")) {
            /*{
                "message" : {
                "code" : 3003,
                "content" : "签名异常"
                },
                "context" : null,
                "success" : false
            }*/
            // 请求失败
            respJson.get("context");
            respJson.get("message");
            return;
        }
        String decrypt = RSAUtils.decryptByPrivateKey(respJson.getString("context"), PayConfig.MCH_PRIVATE_KEY);//商户私钥解密
        JSONObject decryptJson = JSONObject.fromObject(decrypt);//明文数据
        System.out.println(decryptJson.toString());

        // 验签
        if (RSAUtils.verify(decrypt, PayConfig.SHB_PUBLIC_KEY)) {
            System.out.println("验签成功");
        } else {
            System.out.println("验签失败");
        }

        JSONObject respHead = decryptJson.getJSONObject("businessHead");
        JSONObject respContext = decryptJson.getJSONObject("businessContext");
        System.out.println(respHead);
        System.out.println(respContext);

        respContext.get("currenciesType");//币种
        respContext.get("defrayalType");//支付方式
        respContext.get("merchantOrderNumber");//商户订单号
        respContext.get("orderStatus");//订单状态
        respContext.get("shbOrderNumber");//速汇宝订单号
        respContext.get("tradeAmount");//交易金额
        respContext.get("tradeFee");//交易手续费
    }


    /**
     * 请求头
     *
     * @return
     */
    public static JSONObject buildBusinessHead() {
        JSONObject businessHead = new JSONObject();
        businessHead.put("charset", "00");//字符集 00 : utf-8
        businessHead.put("version", "V1.0.0");//版本号
        businessHead.put("method", "payment");//方法
        businessHead.put("merchantNumber", PayConfig.MERCHANT_NUMBER);//商户号
        businessHead.put("requestTime", getSdfTimes());//请求时间
        businessHead.put("signType", "");//签名类型
        businessHead.put("sign", "");//签名
        return businessHead;
    }

    /**
     * 查询订单请求内容
     *
     * @return
     */
    public static JSONObject buildBusinessContextFormQuery() {
        JSONObject businessContext = new JSONObject();
        /** SHB交易单号与商户订单号二选一,优先级：shbOrderNumber >merchantOrderNumber*/
        businessContext.put("shbOrderNumber", "API01180525174339017695000080");//速汇宝订单号
        businessContext.put("merchantOrderNumber", "");//商户订单号
        return businessContext;
    }

    public static String getSdfTimes() {
        return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    }
}
